﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsNode
    {
        public int Add(KeRui.Model.CmsNode model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@NodeName", SqlDbType.NVarChar, 50), new SqlParameter("@NodeKey", SqlDbType.VarChar, 50), new SqlParameter("@NodePath", SqlDbType.VarChar, 500), new SqlParameter("@NodeDir", SqlDbType.NVarChar, 900), new SqlParameter("@SiteID", SqlDbType.Int, 4), new SqlParameter("@ModelID", SqlDbType.Int, 4), new SqlParameter("@ParentID", SqlDbType.Int, 4), new SqlParameter("@Tips", SqlDbType.NVarChar, 0xff), new SqlParameter("@Description", SqlDbType.NText), new SqlParameter("@NodeType", SqlDbType.Int, 4), new SqlParameter("@NodePicUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@NodeLinkUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@MetaKeywords", SqlDbType.NVarChar, 0xff), new SqlParameter("@MetaDescription", SqlDbType.NVarChar, 0xff), new SqlParameter("@ShowOnMenu", SqlDbType.Bit, 1), new SqlParameter("@CreateUser", SqlDbType.NVarChar, 20), 
                new SqlParameter("@TemplateFile", SqlDbType.NVarChar, 280), new SqlParameter("@ContentDefaultTemplateFile", SqlDbType.NVarChar, 280), new SqlParameter("@CommentTemplate", SqlDbType.NVarChar, 280), new SqlParameter("@ContentPageSize", SqlDbType.Int, 4)
            ,new SqlParameter("@ShowSummary",SqlDbType.Bit,1),new SqlParameter("@ShowWords",SqlDbType.Int,2) };
            parameters[0].Value = model.NodeName;
            parameters[1].Value = model.NodeKey;
            parameters[2].Value = model.NodePath;
            parameters[3].Value = model.NodeDir;
            parameters[4].Value = model.SiteID;
            parameters[5].Value = model.ModelID;
            parameters[6].Value = model.ParentID;
            parameters[7].Value = model.Tips;
            parameters[8].Value = model.Description;
            parameters[9].Value = model.NodeType;
            parameters[10].Value = model.NodePicUrl;
            parameters[11].Value = model.NodeLinkUrl;
            parameters[12].Value = model.MetaKeywords;
            parameters[13].Value = model.MetaDescription;
            parameters[14].Value = model.ShowOnMenu;
            parameters[15].Value = model.CreateUser;
            parameters[0x10].Value = model.TemplateFile;
            parameters[0x11].Value = model.ContentDefaultTemplateFile;
            parameters[0x12].Value = model.CommentTemplate;
            parameters[0x13].Value = model.ContentPageSize;
            parameters[0x14].Value = true;
            parameters[0x15].Value = 500;
            return DbHelperSQL.RunProcedure("CmsNode_ADD", parameters, out num);
        }

        public int Delete(int NodeID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int, 4) };
            parameters[0].Value = NodeID;
            return DbHelperSQL.RunProcedure("CmsNode_Delete", parameters, out num);
        }

        public DataSet GetList(int siteID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SiteID", SqlDbType.Int) };
            parameters[0].Value = siteID;
            return DbHelperSQL.RunProcedure("CmsNode_GetListBySiteID", parameters, "ds");
        }

        public KeRui.Model.CmsNode GetModel(int NodeID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int, 4) };
            parameters[0].Value = NodeID;
            KeRui.Model.CmsNode node = new KeRui.Model.CmsNode();
            DataSet set = DbHelperSQL.RunProcedure("CmsNode_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["NodeID"].ToString() != "")
                {
                    node.NodeID = int.Parse(set.Tables[0].Rows[0]["NodeID"].ToString());
                }
                node.NodeName = set.Tables[0].Rows[0]["NodeName"].ToString();
                node.NodeKey = set.Tables[0].Rows[0]["NodeKey"].ToString();
                node.NodePath = set.Tables[0].Rows[0]["NodePath"].ToString();
                node.NodeDir = set.Tables[0].Rows[0]["NodeDir"].ToString();
                if (set.Tables[0].Rows[0]["SiteID"].ToString() != "")
                {
                    node.SiteID = int.Parse(set.Tables[0].Rows[0]["SiteID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ModelID"].ToString() != "")
                {
                    node.ModelID = int.Parse(set.Tables[0].Rows[0]["ModelID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    node.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    node.ParentID = int.Parse(set.Tables[0].Rows[0]["ParentID"].ToString());
                }
                node.ParentPath = set.Tables[0].Rows[0]["ParentPath"].ToString();
                if (set.Tables[0].Rows[0]["Depth"].ToString() != "")
                {
                    node.Depth = int.Parse(set.Tables[0].Rows[0]["Depth"].ToString());
                }
                if (set.Tables[0].Rows[0]["RootID"].ToString() != "")
                {
                    node.RootID = int.Parse(set.Tables[0].Rows[0]["RootID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ChildNum"].ToString() != "")
                {
                    node.ChildNum = int.Parse(set.Tables[0].Rows[0]["ChildNum"].ToString());
                }
                node.arrChildID = set.Tables[0].Rows[0]["arrChildID"].ToString();
                node.allChildID = set.Tables[0].Rows[0]["allChildID"].ToString();
                if (set.Tables[0].Rows[0]["PrevID"].ToString() != "")
                {
                    node.PrevID = int.Parse(set.Tables[0].Rows[0]["PrevID"].ToString());
                }
                if (set.Tables[0].Rows[0]["NextID"].ToString() != "")
                {
                    node.NextID = int.Parse(set.Tables[0].Rows[0]["NextID"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    node.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                node.Tips = set.Tables[0].Rows[0]["Tips"].ToString();
                node.Description = set.Tables[0].Rows[0]["Description"].ToString();
                node.TemplateFile = set.Tables[0].Rows[0]["TemplateFile"].ToString();
                if (set.Tables[0].Rows[0]["NodeType"].ToString() != "")
                {
                    node.NodeType = int.Parse(set.Tables[0].Rows[0]["NodeType"].ToString());
                }
                node.NodePicUrl = set.Tables[0].Rows[0]["NodePicUrl"].ToString();
                node.NodeLinkUrl = set.Tables[0].Rows[0]["NodeLinkUrl"].ToString();
                node.MetaKeywords = set.Tables[0].Rows[0]["MetaKeywords"].ToString();
                node.MetaDescription = set.Tables[0].Rows[0]["MetaDescription"].ToString();
                if (set.Tables[0].Rows[0]["ShowOnMenu"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["ShowOnMenu"].ToString() == "1") || (set.Tables[0].Rows[0]["ShowOnMenu"].ToString().ToLower() == "true"))
                    {
                        node.ShowOnMenu = true;
                    }
                    else
                    {
                        node.ShowOnMenu = false;
                    }
                }
                if (set.Tables[0].Rows[0]["CommentCount"].ToString() != "")
                {
                    node.CommentCount = int.Parse(set.Tables[0].Rows[0]["CommentCount"].ToString());
                }
                if (set.Tables[0].Rows[0]["ContentCount"].ToString() != "")
                {
                    node.ContentCount = int.Parse(set.Tables[0].Rows[0]["ContentCount"].ToString());
                }
                if (set.Tables[0].Rows[0]["ContentPageSize"].ToString() != "")
                {
                    node.ContentPageSize = int.Parse(set.Tables[0].Rows[0]["ContentPageSize"].ToString());
                }
                node.ContentDefaultTemplateFile = set.Tables[0].Rows[0]["ContentDefaultTemplateFile"].ToString();
                node.CommentTemplate = set.Tables[0].Rows[0]["CommentTemplate"].ToString();
                node.CreateUser = set.Tables[0].Rows[0]["CreateUser"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    node.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    node.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                return node;
            }
            return null;
        }

        public KeRui.Model.CmsNode GetModel(int SiteID, string NodePath)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SiteID", SqlDbType.Int, 4), new SqlParameter("@NodePath", SqlDbType.VarChar, 500) };
            parameters[0].Value = SiteID;
            parameters[1].Value = NodePath;
            KeRui.Model.CmsNode node = new KeRui.Model.CmsNode();
            DataSet set = DbHelperSQL.RunProcedure("CmsNode_GetModelbyPath", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["NodeID"].ToString() != "")
                {
                    node.NodeID = int.Parse(set.Tables[0].Rows[0]["NodeID"].ToString());
                }
                node.NodeName = set.Tables[0].Rows[0]["NodeName"].ToString();
                node.NodeKey = set.Tables[0].Rows[0]["NodeKey"].ToString();
                node.NodePath = set.Tables[0].Rows[0]["NodePath"].ToString();
                node.NodeDir = set.Tables[0].Rows[0]["NodeDir"].ToString();
                if (set.Tables[0].Rows[0]["SiteID"].ToString() != "")
                {
                    node.SiteID = int.Parse(set.Tables[0].Rows[0]["SiteID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ModelID"].ToString() != "")
                {
                    node.ModelID = int.Parse(set.Tables[0].Rows[0]["ModelID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ServerID"].ToString() != "")
                {
                    node.ServerID = int.Parse(set.Tables[0].Rows[0]["ServerID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    node.ParentID = int.Parse(set.Tables[0].Rows[0]["ParentID"].ToString());
                }
                node.ParentPath = set.Tables[0].Rows[0]["ParentPath"].ToString();
                if (set.Tables[0].Rows[0]["Depth"].ToString() != "")
                {
                    node.Depth = int.Parse(set.Tables[0].Rows[0]["Depth"].ToString());
                }
                if (set.Tables[0].Rows[0]["RootID"].ToString() != "")
                {
                    node.RootID = int.Parse(set.Tables[0].Rows[0]["RootID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ChildNum"].ToString() != "")
                {
                    node.ChildNum = int.Parse(set.Tables[0].Rows[0]["ChildNum"].ToString());
                }
                node.arrChildID = set.Tables[0].Rows[0]["arrChildID"].ToString();
                node.allChildID = set.Tables[0].Rows[0]["allChildID"].ToString();
                if (set.Tables[0].Rows[0]["PrevID"].ToString() != "")
                {
                    node.PrevID = int.Parse(set.Tables[0].Rows[0]["PrevID"].ToString());
                }
                if (set.Tables[0].Rows[0]["NextID"].ToString() != "")
                {
                    node.NextID = int.Parse(set.Tables[0].Rows[0]["NextID"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    node.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                node.Tips = set.Tables[0].Rows[0]["Tips"].ToString();
                node.Description = set.Tables[0].Rows[0]["Description"].ToString();
                node.TemplateFile = set.Tables[0].Rows[0]["TemplateFile"].ToString();
                if (set.Tables[0].Rows[0]["NodeType"].ToString() != "")
                {
                    node.NodeType = int.Parse(set.Tables[0].Rows[0]["NodeType"].ToString());
                }
                node.NodePicUrl = set.Tables[0].Rows[0]["NodePicUrl"].ToString();
                node.NodeLinkUrl = set.Tables[0].Rows[0]["NodeLinkUrl"].ToString();
                node.MetaKeywords = set.Tables[0].Rows[0]["MetaKeywords"].ToString();
                node.MetaDescription = set.Tables[0].Rows[0]["MetaDescription"].ToString();
                if (set.Tables[0].Rows[0]["ShowOnMenu"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["ShowOnMenu"].ToString() == "1") || (set.Tables[0].Rows[0]["ShowOnMenu"].ToString().ToLower() == "true"))
                    {
                        node.ShowOnMenu = true;
                    }
                    else
                    {
                        node.ShowOnMenu = false;
                    }
                }
                if (set.Tables[0].Rows[0]["CommentCount"].ToString() != "")
                {
                    node.CommentCount = int.Parse(set.Tables[0].Rows[0]["CommentCount"].ToString());
                }
                if (set.Tables[0].Rows[0]["ContentCount"].ToString() != "")
                {
                    node.ContentCount = int.Parse(set.Tables[0].Rows[0]["ContentCount"].ToString());
                }
                if (set.Tables[0].Rows[0]["ContentPageSize"].ToString() != "")
                {
                    node.ContentPageSize = int.Parse(set.Tables[0].Rows[0]["ContentPageSize"].ToString());
                }
                node.ContentDefaultTemplateFile = set.Tables[0].Rows[0]["ContentDefaultTemplateFile"].ToString();
                node.CommentTemplate = set.Tables[0].Rows[0]["CommentTemplate"].ToString();
                node.CreateUser = set.Tables[0].Rows[0]["CreateUser"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    node.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    node.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                return node;
            }
            return null;
        }

        public int sort(int NodeId, int Flag)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeId", SqlDbType.Int, 4), new SqlParameter("@Flag", SqlDbType.Int, 4) };
            parameters[0].Value = NodeId;
            parameters[1].Value = Flag;
            return DbHelperSQL.RunProcedure("CmsNode_Sort", parameters, out num);
        }

        public int Update(KeRui.Model.CmsNode model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@NodeID", SqlDbType.Int, 4), new SqlParameter("@NodeName", SqlDbType.NVarChar, 50), new SqlParameter("@ModelID", SqlDbType.Int, 4), new SqlParameter("@ParentID", SqlDbType.Int, 4), new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@Tips", SqlDbType.NVarChar, 0xff), new SqlParameter("@Description", SqlDbType.NText), new SqlParameter("@TemplateFile", SqlDbType.VarChar, 500), new SqlParameter("@NodeType", SqlDbType.Int, 4), new SqlParameter("@NodePicUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@NodeLinkUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@MetaKeywords", SqlDbType.NVarChar, 0xff), new SqlParameter("@MetaDescription", SqlDbType.NVarChar, 0xff), new SqlParameter("@ShowOnMenu", SqlDbType.Bit, 1), new SqlParameter("@ContentDefaultTemplateFile", SqlDbType.VarChar, 0xff), new SqlParameter("@CommentTemplate", SqlDbType.NVarChar, 280), 
                new SqlParameter("@ContentPageSize", SqlDbType.Int, 4) ,new SqlParameter("@ShowSummary",SqlDbType.Bit,1),new SqlParameter("@ShowWords",SqlDbType.Int,2) 
             };
            parameters[0].Value = model.NodeID;
            parameters[1].Value = model.NodeName;
            parameters[2].Value = model.ModelID;
            parameters[3].Value = model.ParentID;
            parameters[4].Value = model.OrderID;
            parameters[5].Value = model.Tips;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.TemplateFile;
            parameters[8].Value = model.NodeType;
            parameters[9].Value = model.NodePicUrl;
            parameters[10].Value = model.NodeLinkUrl;
            parameters[11].Value = model.MetaKeywords;
            parameters[12].Value = model.MetaDescription;
            parameters[13].Value = model.ShowOnMenu;
            parameters[14].Value = model.ContentDefaultTemplateFile;
            parameters[15].Value = model.CommentTemplate;
            parameters[0x10].Value = model.ContentPageSize;
            parameters[0x11].Value = true;
            parameters[0x12].Value = 500;
            return DbHelperSQL.RunProcedure("CmsNode_Update", parameters, out num);
        }

        public void UpdateNodeInfo(int nodeid, int currentID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@NodeID", SqlDbType.Int), new SqlParameter("@CurrentNodeID", SqlDbType.Int) };
            parameters[0].Value = nodeid;
            parameters[1].Value = currentID;
            DbHelperSQL.RunProcedure("CmsNode_UpdateNodeInfo", parameters);
        }
    }
}

